<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>



    <script>
        var channel = new MessageChannel();
        var port1 = channel.port1;
        var port2 = channel.port2;

        port1.onmessage = function(event) {
            console.log("port1收到数据：" + event.data);
        }
        port2.onmessage = function(event) {
            console.log("port2收到数据：" + event.data);
        }

        port1.postMessage("我是 port1");
        port2.postMessage("我是 port2");
    </script>
    
</body>
</html>